package com.tricky.demo.rabbit.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.tricky.demo.rabbit.service.TestRabbitService;
import com.tricky.demo.rabbit.vo.MessageIdempotent;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


@Component
@Log4j2
public class RabbitCustomerController {

    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * @description:基于数据库,msgId做唯一索引
     * @author: Kevin,Meng
     * @date: 2022/11/24 15:18
     * @param: [msg, channel]
     * @return: void
     **/
/*    @RabbitListener(queues = "na_demo")
    public void receiveMsg(Message msg, Channel channel) throws IOException {
        String messageId = msg.getMessageProperties().getMessageId();
        String messageContent = new String(msg.getBody(), StandardCharsets.UTF_8);
        MessageIdempotent messageIdempotent = new MessageIdempotent();
        LambdaQueryWrapper<MessageIdempotent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MessageIdempotent::getMessageId, messageId);
        MessageIdempotent message = messageIdempotent.selectOne(queryWrapper);

        if (ObjectUtil.isNull(message)) {
            messageIdempotent.setMessageId(messageId);
            messageIdempotent.setMessageContent(messageContent);
            messageIdempotent.insert();
            log.info("na_demo-消费者收到消息，消息ID：" + messageId + " 消息内容：" + messageContent);
            // 消息确认
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
        } else {
            log.info("消息 " + messageId + " 已经消费过！");
        }
    }*/
    /*
     * @description:基于redis实现消息幂等性
     * @author: Kevin,Meng
     * @date: 2022/11/24 15:35
     * @param: [message, channel]
     * @return: void
     **/
    @RabbitListener(queues = "na_demo")
    public void receiveMsgOne(Message message, Channel channel) throws IOException {
        String messageId = message.getMessageProperties().getMessageId();
        String messageContent = new String(message.getBody(), StandardCharsets.UTF_8);
        // 消息不存在则创建，返回 true
        Boolean exist = redisTemplate.opsForValue().setIfAbsent(messageId, messageContent);

        if (!exist) {
            log.info("消息 " + messageId + " 已经消费过");
        } else {
            // 消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("DirectQueue-01-消费者收到消息，消息ID：" + messageId + " 消息内容：" + messageContent);
        }

    }
}
